package com.dmz.wxcp.wecom.wxbusiness.controller;


import com.dmz.wxcp.utils.DataResult;
import com.dmz.wxcp.vo.req.FactoryReqVO;
import com.dmz.wxcp.vo.resp.PageRespVO;
import com.dmz.wxcp.wecom.wxbusiness.entity.Article;
import com.dmz.wxcp.wecom.wxbusiness.mapper.ArticleMapper;
import com.dmz.wxcp.wecom.wxbusiness.mapper.FactoryMapper;
import com.dmz.wxcp.wecom.wxbusiness.service.FactoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/factory")
public class FactoryController {





    @Autowired
    private FactoryService factoryService;

    @Autowired
    private FactoryMapper factoryMapper;

    @Autowired
    private ArticleMapper articleMapper;


    @RequestMapping("/getfactory")
    @ResponseBody
    public DataResult factorylist(@RequestParam Map<String,Object> map){
        FactoryReqVO vo=new FactoryReqVO();
        vo.setTag((map.get("tag").toString()));
        vo.setSearch(map.get("search").toString());
        vo.setCurrentPage(new Long(map.get("page").toString()));
        vo.setPageSize(new Long(map.get("limit").toString()));
        vo.setStartNum((vo.getCurrentPage()-1)*vo.getPageSize());

        List<Object> result=factoryMapper.getFactoryList(vo);
        List<Article> articles=(List<Article>)result.get(0);
        Long count= new Long(((List<Integer>) result.get(1)).get(0));

        PageRespVO<Article> respVO=new PageRespVO<Article>();
        respVO.setRecords(articles);
        respVO.setTotal(count);
//        获取页数  总数除以每页数量
        Long size=count % vo.getPageSize() == 0 ? count/vo.getPageSize() :count/vo.getPageSize()+1;
        respVO.setSize(size);
        return DataResult.success(respVO);
    }


    @GetMapping("/factoryindex")
    public String productindex(Model model){
        Map<String,Object> productCategoryList=factoryService.getTags();
        model.addAttribute("tags",productCategoryList);
        return "factoryindex";
    }


    @RequestMapping("/factoryarticledetail")
    public String articleDetail(@RequestParam Map<String,Object> map,Model model){
        Long id=new Long(map.get("id").toString());
        Article article=articleMapper.getArticleDetail(id);
        String [] imglist=article.getImages().split("\\|_\\|");
        model.addAttribute("article",article);
        model.addAttribute("images",imglist);
        return "factoryarticledetail";
    }


}
